package cn.yeyiu.sexyiu.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.DownloadTabAdapter;
import cn.yeyiu.sexyiu.fragment.DownloadFragment;

public class DownloadActivity extends AppCompatActivity {

//    private DownloadTabAdapter adapter;
    private ImageView backBtn;
    private TextView backTv;

    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    public String[] category;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        dataInit();
        viewInit();
        eventInit();
        uiInit();

    }

    void dataInit(){
        this.category = getResources().getStringArray(R.array.category);
    }

    private void viewInit() {
        backBtn = findViewById(R.id.back_btn);
        backTv = findViewById(R.id.back_tv);
        tabLayout = findViewById(R.id.video_tabs);
        viewPager = findViewById(R.id.video_list_vp);
    }

    private void uiInit() {
        viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        // 不同 tablayout实现方式
//        this.adapter = new DownloadTabAdapter(this);
//        viewPager.setAdapter(adapter);
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return new DownloadFragment(DownloadActivity.this, category[position]);
            }

            @Override
            public int getItemCount() {
                return category.length;
            }
        });

        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(category[position]);
            }
        }).attach();

        // title
        this.backTv.setText(R.string.my_download);

    }

    private void eventInit() {
        this.backBtn.setOnClickListener(v -> {
            finish();
        });

    }
}